package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.entity.PageQueryVo;
import com.itheima.reggie.service.IEmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * <p>
 * 员工信息 前端控制器
 * </p>
 *
 * @author 
 * @since 2024-05-20
 */
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    IEmployeeService employeeService;

    /**
     * 登录功能接口
     * @param employee
     * @param request
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee, HttpServletRequest request){

        return employeeService.login(employee,request);
    }

    /**
     * 退出功能接口
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        //1.清除session里的id 返回结果
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    /**
     *  添加员工功能接口
     * @param employee
     * @param request
     * @return
     */
    @PostMapping
    public R<String> addEmployee(@RequestBody Employee employee,HttpServletRequest request){
       return employeeService.addEmployee(employee,request);
    }

    /**
     * 分页查询功能接口
     * @param pageQueryVo
     * @return
     */
    @GetMapping("page")
    public R<Page> pageQuery(PageQueryVo pageQueryVo){
        log.info("传入的分页参数:{}",pageQueryVo);
        return employeeService.pageQuery(pageQueryVo);
    }

    @PutMapping
    public R<String> updateStatus(@RequestBody Employee employee,HttpServletRequest request){
        log.info("修改员工的id:{}和需要修改成的状态:status:{}",employee.getId(),employee.getStatus());
        Long updateUserId = (Long) request.getSession().getAttribute("employee");
       /* employee.setUpdateUser(updateUserId);
        employee.setUpdateTime(LocalDateTime.now());*/
        employeeService.updateById(employee);
        return R.success("账号状态更改成功!");
    }

    @GetMapping("/{id}")
    public R<Object> queryByIdAndResponse(@PathVariable Long id){
        return employeeService.queryByIdAndResponse(id);
    }

}
